home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus Special 17 / AMIGAplus Sonderheft 17 (1999)(ICP)(DE)[!].iso / mui / developer / c / examples / menus.c < prev    next >
C/C++ Source or Header  |  1977-12-31  |  10KB  |  252 lines

  1. /*
  2. ** This demo shows the use of MUI's menus classes.
  3. */
  4.  
  5. #include "demo.h"
  6.  
  7.  
  8. /*
  9. ** MUI still supports the use of a conventional gadtools NewMenu structure
  10. ** to define a menu strip. The address of this structure is passed to a
  11. ** MUI_MakeObject(MUIO_MenustripNM,newmenu,0) function which creates a complete
  12. ** object tree for this menu and returns a pointer to the menustrip object,
  13. ** This pointer should be placed in a windows MUIA_Window_Menustrip attribute
  14. ** or in an applications MUIA_Application_Menustrip attribute, in which case
  15. ** all windows will inherit the menu. To get direct access to the menu item
  16. ** objects of such an auto generated tree, you can use the MUIM_FindUserData
  17. ** method.
  18. **
  19. ** You can of course create the menu object tree yourself by defining
  20. ** a menu strip object with some menu object children with some menu
  21. ** item object children with optionally some more menu item object
  22. ** children. In this case, use the MUI_MakeObject(MUIO_Menuitem,...)
  23. ** call to create the menu items if possible.
  24. **
  25. ** The main program shows some different methods how to create items and
  26. ** how to react on menu items. Choose the one you like best, and don't
  27. ** forget the MUIM_CallHook method if you dislike switch/case in the
  28. ** main loop!
  29. */
  30.  
  31. #define RB CHECKIT
  32. #define TG CHECKIT|MENUTOGGLE
  33.  
  34. enum { MEN_PROJECT=1,MEN_ABOUT,MEN_QUIT,MEN_EDIT,MEN_CUT,MEN_COPY,MEN_PASTE,MEN_SETTINGS,MEN_HARDWARE,MEN_A1000,MEN_A2000,MEN_A3000,MEN_A4000,MEN_SOFTWARE,MEN_MUI,MEN_MFR,MEN_MWB,MEN_DFA,ID_ADD,ID_REM,ID_RADIO,ID_TOG };
  35.  
  36. static struct NewMenu MenuData1[] =
  37. {
  38.     { NM_TITLE, "Project"                  , 0 ,0 ,0             ,(APTR)MEN_PROJECT  },
  39.     { NM_ITEM ,  "About"                   ,"?",0 ,0             ,(APTR)MEN_ABOUT    },
  40.     { NM_ITEM ,  NM_BARLABEL               , 0 ,0 ,0             ,(APTR)0            },
  41.     { NM_ITEM ,  "Quit"                    ,"Q",0 ,0             ,(APTR)MEN_QUIT     },
  42.  
  43.     { NM_TITLE, "Edit"                     , 0 ,0 ,0             ,(APTR)MEN_EDIT     },
  44.     { NM_ITEM ,  "Cut"                     ,"X",0 ,0             ,(APTR)MEN_CUT      },
  45.     { NM_ITEM ,  "Copy"                    ,"C",0 ,0             ,(APTR)MEN_COPY     },
  46.     { NM_ITEM ,  "Paste"                   ,"V",0 ,0             ,(APTR)MEN_PASTE    },
  47.  
  48.     { NM_TITLE, "Settings"                 , 0 ,NM_MENUDISABLED,0,(APTR)MEN_SETTINGS },
  49.     { NM_ITEM ,  "Hardware"                , 0 ,NM_ITEMDISABLED,0,(APTR)MEN_HARDWARE },
  50.     { NM_SUB  ,   "A1000"                  ,"1",RB|CHECKED,2|4|8 ,(APTR)MEN_A1000    },
  51.     { NM_SUB  ,   "A2000"                  ,"2",RB,1|4|8         ,(APTR)MEN_A2000    },
  52.     { NM_SUB  ,   "A3000"                  ,"3",RB,1|2|8         ,(APTR)MEN_A3000    },
  53.     { NM_SUB  ,   "A4000"                  ,"4",RB,1|2|4         ,(APTR)MEN_A4000    },
  54.     { NM_ITEM ,  "Software"                , 0 ,NM_ITEMDISABLED,0,(APTR)MEN_SOFTWARE },
  55.     { NM_SUB  ,   "MUI"                    ,"M",TG,0             ,(APTR)MEN_MUI      },
  56.     { NM_SUB  ,   "MFR"                    ,"F",TG,0             ,(APTR)MEN_MFR      },
  57.     { NM_SUB  ,   "MagicWB"                ,"W",TG,0             ,(APTR)MEN_MWB      },
  58.     { NM_SUB  ,   "DFA"                    ,"D",TG,0             ,(APTR)MEN_DFA      },
  59.  
  60.     { NM_END,NULL,0,0,0,(APTR)0 },
  61. };
  62.  
  63.  
  64. int main(int argc,char *argv[])
  65. {
  66.     APTR app,win1,cm1,cm2,cm3,cm4,radio,cy1,cy2,cy3,menuset,menuedit,itemhw,itemsw,strip,menumisc,btadd,btrem,bttog,possi;
  67.     ULONG signals,x;
  68.     BOOL running = TRUE;
  69.     BOOL addedmisc = FALSE;
  70.     static char *hardware[] = { "Amiga 1000", "Amiga 2000", "Amiga 3000", "Amiga 4000", NULL };
  71.     static char *endis1[]   = { "Settings disabled", "Settings enabled", NULL };
  72.     static char *endis2[]   = { "Hardware disabled", "Hardware enabled",  NULL };
  73.     static char *endis3[]   = { "Software disabled", "Software enabled",  NULL };
  74.  
  75.     init();
  76.  
  77.     app = ApplicationObject,
  78. /*
  79.         MUIA_Application_Title      , "Menus",
  80.         MUIA_Application_Version    , "$VER: Menus 17.6 (18.08.96)",
  81.         MUIA_Application_Copyright  , "©1994 by Stefan Stuntz",
  82.         MUIA_Application_Author     , "Stefan Stuntz",
  83.         MUIA_Application_Description, "Demonstrate MUI's menu classes.",
  84.         MUIA_Application_Base       , "MENU",
  85. */
  86.         MUIA_Application_Window, win1 = WindowObject,
  87.             MUIA_Window_Title, "Menus",
  88.             MUIA_Window_ID   , MAKE_ID('M','E','N','1'),
  89.             MUIA_Window_Menustrip, strip = MUI_MakeObject(MUIO_MenustripNM,MenuData1,0),
  90.             WindowContents, VGroup,
  91.                 Child, cy1 = MUI_MakeObject(MUIO_Cycle,NULL,endis1),
  92.                 Child, HGroup, MUIA_Group_SameSize, TRUE,
  93.                     Child, cy2 = MUI_MakeObject(MUIO_Cycle,NULL,endis2),
  94.                     Child, cy3 = MUI_MakeObject(MUIO_Cycle,NULL,endis3),
  95.                     End,
  96.                 Child, MUI_MakeObject(MUIO_HBar,4),
  97.                 Child, HGroup,
  98.                     Child, HSpace(0),
  99.                     Child, radio = MUI_MakeObject(MUIO_Radio,NULL,hardware),
  100.                     Child, HSpace(0),
  101.                     Child, MUI_MakeObject(MUIO_VBar,1),
  102.                     Child, HSpace(0),
  103.                     Child, ColGroup(2),
  104.                         Child, Label("_MUI"    ), Child, cm1 = MUI_MakeObject(MUIO_Checkmark,"_MUI"    ),
  105.                         Child, Label("M_FR"    ), Child, cm2 = MUI_MakeObject(MUIO_Checkmark,"M_FR"    ),
  106.                         Child, Label("Magic_WB"), Child, cm3 = MUI_MakeObject(MUIO_Checkmark,"Magic_WB"),
  107.                         Child, Label("_DFA"    ), Child, cm4 = MUI_MakeObject(MUIO_Checkmark,"_DFA"    ),
  108.                         End,
  109.                     Child, HSpace(0),
  110.                     End,
  111.                 Child, MUI_MakeObject(MUIO_HBar,4),
  112.                 Child, HGroup, MUIA_Group_SameSize, TRUE,
  113.                     Child, btadd = MUI_MakeObject(MUIO_Button,"_Add Misc Menu"),
  114.                     Child, btrem = MUI_MakeObject(MUIO_Button,"_Remove Misc Menu"),
  115.                     Child, bttog = MUI_MakeObject(MUIO_Button,"_Toggle Misc Titles"),
  116.                     End,
  117.                 End,
  118.             End,
  119.         End;
  120.  
  121.     menumisc = MenuObject, MUIA_Menu_Title, "Misc",
  122.         MUIA_Family_Child, MUI_MakeObject(MUIO_Menuitem,"Dynamic" ,0,0,0),
  123.         MUIA_Family_Child, MUI_MakeObject(MUIO_Menuitem,"Adding"  ,0,0,0),
  124.         MUIA_Family_Child, MUI_MakeObject(MUIO_Menuitem,"Of"      ,0,0,0),
  125.         MUIA_Family_Child, MUI_MakeObject(MUIO_Menuitem,"Items"   ,0,0,0),
  126.         MUIA_Family_Child, MUI_MakeObject(MUIO_Menuitem,"Is"      ,0,0,0),
  127.         MUIA_Family_Child, possi = MUI_MakeObject(MUIO_Menuitem,"Possible",0,CHECKIT|CHECKED|MENUTOGGLE,0),
  128.         End;
  129.  
  130.     if (!app || !menumisc)
  131.         fail(app,"Failed to create Application.");
  132.  
  133.     menuset  = (APTR)DoMethod(strip,MUIM_FindUData,MEN_SETTINGS);
  134.     menuedit = (APTR)DoMethod(strip,MUIM_FindUData,MEN_EDIT);
  135.     itemhw   = (APTR)DoMethod(strip,MUIM_FindUData,MEN_HARDWARE);
  136.     itemsw   = (APTR)DoMethod(strip,MUIM_FindUData,MEN_SOFTWARE);
  137.  
  138.     DoMethod(cy1,MUIM_Notify,MUIA_Cycle_Active,MUIV_EveryTime,menuset,3,MUIM_Set,MUIA_Menu_Enabled    ,MUIV_TriggerValue);
  139.     DoMethod(cy2,MUIM_Notify,MUIA_Cycle_Active,MUIV_EveryTime,itemhw ,3,MUIM_Set,MUIA_Menuitem_Enabled,MUIV_TriggerValue);
  140.     DoMethod(cy3,MUIM_Notify,MUIA_Cycle_Active,MUIV_EveryTime,itemsw ,3,MUIM_Set,MUIA_Menuitem_Enabled,MUIV_TriggerValue);
  141.  
  142.     DoMethod(btadd,MUIM_Notify,MUIA_Pressed,FALSE,app,2,MUIM_Application_ReturnID,ID_ADD);
  143.     DoMethod(btrem,MUIM_Notify,MUIA_Pressed,FALSE,app,2,MUIM_Application_ReturnID,ID_REM);
  144.     DoMethod(bttog,MUIM_Notify,MUIA_Pressed,FALSE,app,2,MUIM_Application_ReturnID,ID_TOG);
  145.  
  146.     DoMethod(app,MUIM_Notify,MUIA_Application_MenuAction,MEN_A1000,radio,3,MUIM_Set,MUIA_Radio_Active,0);
  147.     DoMethod(app,MUIM_Notify,MUIA_Application_MenuAction,MEN_A2000,radio,3,MUIM_Set,MUIA_Radio_Active,1);
  148.     DoMethod(app,MUIM_Notify,MUIA_Application_MenuAction,MEN_A3000,radio,3,MUIM_Set,MUIA_Radio_Active,2);
  149.     DoMethod(app,MUIM_Notify,MUIA_Application_MenuAction,MEN_A4000,radio,3,MUIM_Set,MUIA_Radio_Active,3);
  150.  
  151.     DoMethod(cm1,MUIM_Notify,MUIA_Selected,MUIV_EveryTime,strip,4,MUIM_SetUData,MEN_MUI,MUIA_Menuitem_Checked,MUIV_TriggerValue);
  152.     DoMethod(cm2,MUIM_Notify,MUIA_Selected,MUIV_EveryTime,strip,4,MUIM_SetUData,MEN_MFR,MUIA_Menuitem_Checked,MUIV_TriggerValue);
  153.     DoMethod(cm3,MUIM_Notify,MUIA_Selected,MUIV_EveryTime,strip,4,MUIM_SetUData,MEN_MWB,MUIA_Menuitem_Checked,MUIV_TriggerValue);
  154.     DoMethod(cm4,MUIM_Notify,MUIA_Selected,MUIV_EveryTime,strip,4,MUIM_SetUData,MEN_DFA,MUIA_Menuitem_Checked,MUIV_TriggerValue);
  155.  
  156.     DoMethod(radio,MUIM_Notify,MUIA_Radio_Active,MUIV_EveryTime,app,2,MUIM_Application_ReturnID,ID_RADIO);
  157.  
  158.     DoMethod(win1,MUIM_Notify,MUIA_Window_CloseRequest,TRUE,app,2,MUIM_Application_ReturnID,MUIV_Application_ReturnID_Quit);
  159.  
  160.     set(btrem,MUIA_Disabled,TRUE);
  161.  
  162.     set(cm1,MUIA_UserData,MEN_MUI);
  163.     set(cm2,MUIA_UserData,MEN_MFR);
  164.     set(cm3,MUIA_UserData,MEN_MWB);
  165.     set(cm4,MUIA_UserData,MEN_DFA);
  166.  
  167.  
  168. /*
  169. ** Input loop...
  170. */
  171.  
  172.     set(win1,MUIA_Window_Open,TRUE);
  173.  
  174.     while (running)
  175.     {
  176.         ULONG id = DoMethod(app,MUIM_Application_Input,&signals);
  177.  
  178.         switch (id)
  179.         {
  180.             case MEN_QUIT:
  181.             case MUIV_Application_ReturnID_Quit:
  182.                 running = FALSE;
  183.                 break;
  184.  
  185.             case MEN_ABOUT:
  186.                 MUI_Request(app,win1,0,NULL,"OK","Some little about window.");
  187.                 break;
  188.  
  189.             case ID_ADD:
  190.                 set(btadd,MUIA_Disabled,TRUE);
  191.                 set(btrem,MUIA_Disabled,FALSE);
  192.                 DoMethod(strip,MUIM_Family_Insert,menumisc,menuedit);
  193.                 addedmisc = TRUE;
  194.                 break;
  195.  
  196.             case ID_REM:
  197.                 set(btadd,MUIA_Disabled,FALSE);
  198.                 set(btrem,MUIA_Disabled,TRUE);
  199.                 DoMethod(strip,MUIM_Family_Remove,menumisc);
  200.                 addedmisc = FALSE;
  201.                 break;
  202.  
  203.             case ID_TOG:
  204.             {
  205.                 char *x;
  206.  
  207.                 get(menumisc,MUIA_Menu_Title,&x);
  208.                 if (strcmp(x,"Misc"))
  209.                     set(menumisc,MUIA_Menu_Title,"Misc");
  210.                 else
  211.                     set(menumisc,MUIA_Menu_Title,"Miscodil");
  212.  
  213.                 get(possi,MUIA_Menuitem_Title,&x);
  214.                 if (strcmp(x,"Possible"))
  215.                     set(possi,MUIA_Menuitem_Title,"Possible");
  216.                 else
  217.                     set(possi,MUIA_Menuitem_Title,"elbissoP");
  218.  
  219.                 break;
  220.             }
  221.  
  222.             case MEN_MUI:
  223.             case MEN_MFR:
  224.             case MEN_MWB:
  225.             case MEN_DFA:
  226.                 DoMethod(strip,MUIM_GetUData,id,MUIA_Menuitem_Checked,&x);
  227.                 DoMethod(win1,MUIM_SetUData,id,MUIA_Selected,x);
  228.                 break;
  229.  
  230.             case ID_RADIO:
  231.                 get(radio,MUIA_Radio_Active,&x);
  232.                 DoMethod(itemhw,MUIM_SetUData,MEN_A1000,MUIA_Menuitem_Checked,x==0);
  233.                 DoMethod(itemhw,MUIM_SetUData,MEN_A2000,MUIA_Menuitem_Checked,x==1);
  234.                 DoMethod(itemhw,MUIM_SetUData,MEN_A3000,MUIA_Menuitem_Checked,x==2);
  235.                 DoMethod(itemhw,MUIM_SetUData,MEN_A4000,MUIA_Menuitem_Checked,x==3);
  236.                 break;
  237.         }
  238.         if (running && signals) Wait(signals);
  239.     }
  240.  
  241.     set(win1,MUIA_Window_Open,FALSE);
  242.  
  243. /*
  244. ** Shut down...
  245. */
  246.  
  247.     if (!addedmisc)
  248.         MUI_DisposeObject(menumisc);
  249.  
  250.     fail(app,NULL);
  251. }
  252.